﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ETC.DLEDLightingSystemPlus.Helpers
{
    public class EncryptConverter : JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (value is string str)
            {
                writer.WriteValue(Bandit.Helpers.CryptionHelper.DESEncrypt(str));
            }
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            try
            {
                if (reader.TokenType == JsonToken.String)
                {
                    string str = reader.Value.ToString();

                    string value = Bandit.Helpers.CryptionHelper.DESDecrypt(str);

                    return value;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("EncryptConverter.ReadJson : " + ex.Message);
            }

            return ReadJson(reader, objectType, existingValue, serializer);
        }

        public override bool CanConvert(Type objectType)
        {
            return true;
        }
    }

    public class JsonHelper
    {
        /// <summary>
        /// 将对象序列化为Json文件
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="o">对象</param>
        /// <param name="filePath">存放路径</param>
        public static void FileSerialize<T>(T o, string filePath)
        {
            string dir = Path.GetDirectoryName(filePath);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            JsonSerializer serializer = new JsonSerializer();
            serializer.Formatting = Formatting.Indented;
            StreamWriter sw = new StreamWriter(filePath, false);
            serializer.Serialize(sw, o);
            sw.Flush();
            sw.Close();
        }

        /// <summary>
        /// 将Json文件反序列为为对象
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="filePath">要反序列化的文件</param>
        /// <returns>T</returns>
        public static T FileDeserialize<T>(string filePath)
        {
            JsonSerializer serializer = new JsonSerializer();
            StreamReader sr = new StreamReader(filePath);
            T o = (T)serializer.Deserialize(sr, typeof(T));
            sr.Close();
            return o;
        }

        /// <summary>
        /// 将对象序列化为Json文本
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="o">对象</param>
        /// <returns>Xml文本</returns>
        public static string TextSerialize(object obj, bool isIgnoreNull = false)
        {
            string text = string.Empty;

            JsonSerializerSettings setting = new JsonSerializerSettings()
            {
                NullValueHandling = isIgnoreNull ? NullValueHandling.Ignore : NullValueHandling.Include,
                Formatting = Formatting.Indented
            };

            if (obj != null)
            {
                text = JsonConvert.SerializeObject(obj, setting);
            }
            else
            {
                text = "{}";
            }

            return text;
        }

        /// <summary>
        /// 将Json文本反序列为为对象
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="text">要反序列化的文本</param>
        /// <returns>T</returns>
        public static T TextDeserialize<T>(string text)
        {
            T o = (T)JsonConvert.DeserializeObject(text);
            return o;
        }
    }
}
